package com.sunny.interceptor;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * Created by tongchengke on 2016/8/23.
 */

@Controller
@Scope("prototype")
public class LoginInterceptor extends AbstractInterceptor {

    @Override
    public void init() {
        System.out.println("LoginInterceptor:init--->>>" + System.currentTimeMillis());
    }

    @Override
    public void destroy() {
        System.out.println("LoginInterceptor:destroy--->>>" + System.currentTimeMillis());
    }

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {

        ActionContext actionContext = actionInvocation.getInvocationContext();
        Map<String, Object> session = actionContext.getSession();
        Object user = session.get("user");

        if(user!=null){
            System.out.println("通过.....");
            return actionInvocation.invoke();
        }
        return Action.LOGIN;
    }
}
